/*
 * @Description:
 * @Version: 2.0
 * @Date: 2021-08-24 15:00:56
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-08-30 16:17:16
 */
/*
 * @lc app=leetcode.cn id=94 lang=javascript
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
// var inorderTraversal = function (root) {
//   if (root == null) {
//     return [];
//   }
//   let inorderTraversalArry = [];
//   inorderTraversalArry = inorderTraversalArry.concat(
//     inorderTraversal(root.left)
//   );
//   inorderTraversalArry.push(root.val);
//   inorderTraversalArry = inorderTraversalArry.concat(
//     inorderTraversal(root.right)
//   );
//   return inorderTraversalArry;
// };

// @lc code=end

/**
 * @name:
 * @description:
 * @param {TreeNode} root
 * @return {number[]}
 * @LastEditors:
 */
var inorderTraversal = function (root) {
  if (root == null) return [];
  let stack = [];
  let inorderTraversalArry = [];
  while (stack.length || root) {
    if (root != null) {
      stack.push(root);
      root = root.left;
    } else {
      root = stack.pop();
      inorderTraversalArry.push(root.val);
      root = root.right;
    }
  }
  return inorderTraversalArry;
};
